﻿using System.Web;

namespace YanZhiwei.DotNet2.Utilities.AspNet
{
    /// <summary>
    /// session 帮助类
    /// </summary>
    public class SessionHelper
    {

        #region 根据session名获取session对象
        /// <summary>
        /// 根据session名获取session对象
        /// </summary>
        /// <param name="name">键</param>
        /// <returns>值</returns>
        public static object GetSession(string name)
        {
            return HttpContext.Current.Session[name];
        }
        #endregion
        #region 设置session
        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="name">键</param>
        /// <param name="value">值</param>
        public static void SetSession(string name, object value)
        {
            HttpContext.Current.Session.Remove(name);
            HttpContext.Current.Session.Add(name, value);
        }
        #endregion
        #region 添加Session，调动有效期为20分钟
        /// <summary>
        /// 添加Session，调动有效期为20分钟
        /// </summary>
        /// <param name="name">键</param>
        /// <param name="value">值</param>
        public static void Add(string name, string value)
        {
            HttpContext.Current.Session[name] = value;
            HttpContext.Current.Session.Timeout = 20;
        }
        /// <summary>
        /// 添加Session，调动有效期为20分钟
        /// </summary>
        /// <param name="sessionName">键</param>
        /// <param name="sessionValues">值</param>
        public static void Adds(string sessionName, string[] sessionValues)
        {
            HttpContext.Current.Session[sessionName] = sessionValues;
            HttpContext.Current.Session.Timeout = 20;
        }
        #endregion
        #region 添加Session
        /// <summary>
        /// 添加Session
        /// </summary>
        /// <param name="sessionName">Session对象名称</param>
        /// <param name="sessionValue">Session值</param>
        /// <param name="iExpires">调动有效期（分钟）</param>
        public static void Add(string sessionName, string sessionValue, int iExpires)
        {
            HttpContext.Current.Session[sessionName] = sessionValue;
            HttpContext.Current.Session.Timeout = iExpires;
        }
        /// <summary>
        /// 添加Session
        /// </summary>
        /// <param name="sessionName">Session对象名称</param>
        /// <param name="sessionValues">Session值数组</param>
        /// <param name="iExpires">调动有效期（分钟）</param>
        public static void Adds(string sessionName, string[] sessionValues, int iExpires)
        {
            HttpContext.Current.Session[sessionName] = sessionValues;
            HttpContext.Current.Session.Timeout = iExpires;
        }
        #endregion
        #region 读取某个Session对象值
        /// <summary>
        /// 读取某个Session对象值
        /// </summary>
        /// <param name="sessionName">Session对象名称</param>
        /// <returns>Session对象值</returns>
        public static string Get(string sessionName)
        {
            if (HttpContext.Current.Session[sessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[sessionName].ToString();
            }
        }
        /// <summary>
        /// 读取某个Session对象值数组
        /// </summary>
        /// <param name="sessionName">Session对象名称</param>
        /// <returns>Session对象值数组</returns>
        public static string[] Gets(string sessionName)
        {
            if (HttpContext.Current.Session[sessionName] == null)
            {
                return null;
            }
            else
            {
                return (string[])HttpContext.Current.Session[sessionName];
            }
        }
        #endregion 
        #region 删除某个Session对象
        /// <summary>
        /// 删除某个Session对象
        /// </summary>
        /// <param name="sessionName">Session对象名称</param>
        public static void Del(string sessionName)
        {
            HttpContext.Current.Session[sessionName] = null;
        }
        #endregion 
    }
}
